﻿$(document).ready(function () {
    $(document).on("blur", "table[id=ctl00_ContentPlaceHolder3_td_controlgrid] input", function () {
        getControlValues($(this), true);
    });
    $(document).on("change", "table[id=ctl00_ContentPlaceHolder3_td_controlgrid] select", function () {
        getControlValues($(this), true);
    });

    $(document).on("blur", "div[id=div_controlproperty] input", function () {
        getControlValues($(this), false);
    });
    $(document).on("change", "div[id=div_controlproperty] select", function () {
        getControlValues($(this), false);
    });


    $(document).on("click", "table[id=ctl00_ContentPlaceHolder3_td_controlgrid] tr", function () {
        var cur_index = $(this).index() - 1;
        $("#ctl00_ContentPlaceHolder3_hf_currentindex").val(cur_index);
        loadControlProperties(cur_index);
    });

    $(document).on("click", "img[id^=ctl00_ContentPlaceHolder3_img_delete_]", function () {
        deleteResetControl($(this));
    });

    $(document).on("click", "img[id^=ctl00_ContentPlaceHolder3_img_property_]", function () {
        showHideDIVcontrolproeprty($(this));
    });

    $(document).on("keyup", "input[id^=ctl00_ContentPlaceHolder3_txt_name]", function () {
        setNameControlDeleteProperty($(this))
    });

    $(document).on("blur", "input[id^=ctl00_ContentPlaceHolder3_txt_name]", function () {
        setNameControlDeleteProperty($(this))
    });
    //$("#ctl00_ContentPlaceHolder3_lblMessage").css("display", "none");
});

function setNameControlDeleteProperty(name) {
    var cur_index = $(name[0]).closest('td').parent()[0].sectionRowIndex - 1;
    if ($("#ctl00_ContentPlaceHolder3_hf_currentindex").val() != cur_index) {
        loadControlProperties(cur_index);
        $("#ctl00_ContentPlaceHolder3_hf_currentindex").val(cur_index);
    }
    var mb_url = window.location.href.substr(window.location.href.lastIndexOf('/') + 1).toLowerCase();

    var obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
    
    //indexOf returns the position of the string in the other string. If not found, it will return -1:
    if (mb_url.indexOf("pk=") == -1 || mb_url.indexOf("cpy=") != -1 || obj_json.CONTROLDETAILS[cur_index]["txt_name"] == null || obj_json.CONTROLDETAILS[cur_index]["txt_name"] == "") {
        var value = name.val();
        var SplChar = "=\\;.:,+*\'/!-\"#";
        //char[] trim = { '=', '\\', ';', '.', ':', ',', '+', '*', '\'', '/' };
        for (var i = 0; i < SplChar.length; i++) {
            if (value.indexOf(SplChar[i]) != -1)
                value = value.replace(SplChar[i], "");
        }
        $("#ctl00_ContentPlaceHolder3_txt_control_id").val(value.replace(" ", "_"));
    }
    if (name.val() != "")
        $("#ctl00_ContentPlaceHolder3_img_delete_" + cur_index).css("display", "inline-block");
}

function showHideDIVpagesettings() {
    if ($("#div_pagesettings").is(":hidden"))
    {
        $("#img_pagesettings").attr("title", "Click to collapse");
        $("#div_pagesettings").slideDown();
        $("#img_pagesettings").attr("src", "Images/collapse3.gif");
    }
    else {
        $("#img_pagesettings").attr("title", "Click to expand");
        $("#div_pagesettings").slideUp();
        $("#img_pagesettings").attr("src", "Images/expand3.gif");
    }
}

function showHideDIVroleSecurity() {
    if ($("#div_rolesecurity").is(":hidden")) {
        $("#img_rolesecurity").attr("title", "Click to collapse");
        $("#div_rolesecurity").slideDown();
        $("#img_rolesecurity").attr("src", "Images/collapse3.gif");
    }
    else {
        $("#img_rolesecurity").attr("title", "Click to expand");
        $("#div_rolesecurity").slideUp();
        $("#img_rolesecurity").attr("src", "Images/expand3.gif");
    }
}

//To Show / Hide the proeprty window with appropriate value, and get the current index
function showHideDIVcontrolproeprty(event) {
    if ($("#div_controlproperty").is(":hidden")) 
        $("#div_controlproperty").show("slide", { direction: "right" }, "fast");
//    else
//        $("#div_controlproperty").hide("slide", { direction: "right" }, "fast");
}

//To Hide the proeprty window
function Hidecontrolproeprty() {
    if (!$("#div_controlproperty").is(":hidden"))
        $("#div_controlproperty").hide("slide", { direction: "right" }, "fast");
}

function showpageicon() {
    var selectedtext = "";
    try {
        if (document.getElementById("ctl00_ContentPlaceHolder3_ddl_icon") != null) {
            selectedtext = document.getElementById("ctl00_ContentPlaceHolder3_ddl_icon").value;
        }
        if (selectedtext != "") {
            if (document.getElementById("ctl00_ContentPlaceHolder3_ImgIcon") != null) {
                document.getElementById("ctl00_ContentPlaceHolder3_ImgIcon").src = "Images/MBIcons/medium/" + selectedtext;
            }
        }
    }
    catch (err) {}
}

function showParentMenu() {
    var mb_url = window.location.href.substr(window.location.href.lastIndexOf('/') + 1).toLowerCase();
    //indexOf returns the position of the string in the other string. If not found, it will return -1:
    if (mb_url.indexOf("pk=") == -1 || mb_url.indexOf("cpy=") != -1) {
        if ($("#td_parent_menu").is(":hidden"))
            $("#td_parent_menu").css("display", "block");
        else
            $("#td_parent_menu").css("display", "none");
    }
}

function getDescFromName(cur_obj) {
    if (cur_obj.value == "") {
        var cur_id = cur_obj.id;
        var cur_index = cur_id.substr(cur_id.lastIndexOf('_'));
        document.getElementById(cur_id).value = $("#ctl00_ContentPlaceHolder3_txt_name" + cur_index).val();
        cur_obj.select();
    }
}

function getAdditionalControl() {
    var types = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controltype_datatype").val());
    var index = $("#ctl00_ContentPlaceHolder3_hf_NoOfControls").val();
    if (types != null) {
        if (types.hasOwnProperty("CONTROLTYPE") && types.hasOwnProperty("DATATYPE")) {
            this.controltype = types.CONTROLTYPE;
            this.datatype = types.DATATYPE;
            var trObj = document.createElement('tr');
            var tdObj = document.createElement('td');
            
            var txt_name = document.createElement('input');
            txt_name.type = 'text';
            txt_name.id = "ctl00_ContentPlaceHolder3_txt_name_" + index;
            txt_name.className = "controlFont";
            tdObj.appendChild(txt_name);
            trObj.appendChild(tdObj);

            tdObj = document.createElement('td');
            var txt_desc = document.createElement('input');
            txt_desc.type = 'text';
            txt_desc.id = "ctl00_ContentPlaceHolder3_txt_desc_" + index;
            txt_desc.className = "controlFont";
            txt_desc.setAttribute("onfocus", "javascript:getDescFromName(this);");
            tdObj.appendChild(txt_desc);
            trObj.appendChild(tdObj);

            tdObj = document.createElement('td');
            var txt_seq = document.createElement('input');
            txt_seq.type = 'text';
            txt_seq.id = "ctl00_ContentPlaceHolder3_txt_seq_" + index;
            txt_seq.className = "controlFont";
            txt_seq.style.width = "60px";
            tdObj.appendChild(txt_seq);
            trObj.appendChild(tdObj);

            tdObj = document.createElement('td');
            var ddl_ctr_type = document.createElement('select');
            ddl_ctr_type.id = "ctl00_ContentPlaceHolder3_ddl_ctr_type_" + index;
            ddl_ctr_type.className = "controlFont";
            ddl_ctr_type.setAttribute("style", "width:80px;");
            for (var i = 0; i < controltype.length; i++) {
                var op = new Option();
                op.value = controltype[i]["mem_id"];
                op.text = controltype[i]["mem_name"];
                if (controltype[i]["mem_id"].toString() == "TB")
                    op.selected = true;
                ddl_ctr_type.options.add(op);    
            }
            tdObj.appendChild(ddl_ctr_type);
            trObj.appendChild(tdObj);

            tdObj = document.createElement('td');
            var ddl_data_type = document.createElement('select');
            ddl_data_type.id = "ctl00_ContentPlaceHolder3_ddl_data_type_" + index;
            ddl_data_type.className = "controlFont";
            ddl_data_type.setAttribute("style", "width:80px;");
            for (var i = 0; i < datatype.length; i++) {
                var op = new Option();
                op.value = datatype[i]["mem_id"];
                op.text = datatype[i]["mem_name"];
                if (datatype[i]["mem_id"].toString() == "TX")
                    op.selected = true;
                ddl_data_type.options.add(op);
            }
            tdObj.appendChild(ddl_data_type);
            trObj.appendChild(tdObj);

            tdObj = document.createElement('td');
            tdObj.setAttribute("align", "right");
            tdObj.innerHTML = "<img style=\"border-width:0px;cursor:pointer; display:none;\" alt=\"Delete Control\" src=\"Images/control_delete.png\" title=\"Delete Control\" id=\"ctl00_ContentPlaceHolder3_img_delete_" + index + "\"/>&nbsp;";
            tdObj.innerHTML = tdObj.innerHTML + "<img style=\"border-width:0px;cursor:pointer;\" alt=\"Control Property\" src=\"Images/control_property.png\" title=\"Control Property\" id=\"ctl00_ContentPlaceHolder3_img_property_" + index + "\"/>&nbsp;";
            trObj.appendChild(tdObj);

            $("table[id=ctl00_ContentPlaceHolder3_td_controlgrid] tbody:first-child").append(trObj);
            document.getElementById('ctl00_ContentPlaceHolder3_hf_NoOfControls').value = (parseInt(index) + 1).toString();

            var obj_json = new Array();
            if ($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val() != "")
                obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
            if (obj_json.hasOwnProperty("CONTROLDETAILS"))
                if (obj_json.CONTROLDETAILS.length <= index) {
                    addtojson(index, obj_json.CONTROLDETAILS);
                    document.getElementById('ctl00_ContentPlaceHolder3_hf_controldetails_json').value = JSON.stringify(obj_json);
                }
        }
    }
}

function getControlValues(obj, isGrid) {
    try {
        var obj_json = new Array();
        var json_var;
        var cur_id = obj[0].id;
        var cur_index = cur_id.substr(cur_id.lastIndexOf('_') + 1);
        //var cur_index = $(obj[0]).closest('td').parent()[0].sectionRowIndex - 1;
        if (!isGrid) {
            cur_index = $("#ctl00_ContentPlaceHolder3_hf_currentindex").val();
            json_var = cur_id.replace("ctl00_ContentPlaceHolder3_", "");
        }
        else {
            $("#ctl00_ContentPlaceHolder3_hf_currentindex").val(cur_index);
            json_var = cur_id.substr(0, cur_id.lastIndexOf('_')).replace("ctl00_ContentPlaceHolder3_", "");
        }
        if ($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val() == "")
            addtojson(cur_index, obj_json);
        else
            obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
        if (obj_json.hasOwnProperty("CONTROLDETAILS"))
            if (obj_json.CONTROLDETAILS.length < cur_index)
                addtojson(cur_index, obj_json.CONTROLDETAILS);
        obj_json.CONTROLDETAILS[cur_index][json_var] = $.trim(obj.val());
        $("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val(JSON.stringify(obj_json));
    }
    catch (e) { }
}

function addtojson(index, obj_json) {
    obj_json[index] = { "pg_ctr_row_id": "", "obj_att_row_id": "", "ctr_owner": "", "txt_name": "", "txt_desc": "", "txt_seq": "", "ddl_ctr_type": "", "ddl_data_type": "", "txt_control_id": "", "ddl_visible": "", "ddl_is_read": "", "ddl_in_list": "", "txt_size": "", "txt_default_value": "", "ddl_is_unique": "", "ddl_filter": "", "txt_tooltip": "", "ddl_data_bind": "", "ddl_format_mask": "", "ddl_new_section": "", "ddl_search": "", "txt_section_name": "", "ddl_field_type": "", "ta_droplist": "" };
}

function loadControlProperties(cur_index) {
    var obj_json = new Array();
    if ($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val() == "") return;
    obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
    if (obj_json.hasOwnProperty("CONTROLDETAILS"))
        if (obj_json.CONTROLDETAILS.length <= cur_index)
            addtojson(cur_index, obj_json.CONTROLDETAILS);
    if (obj_json.CONTROLDETAILS[cur_index]["txt_control_id"] == null || obj_json.CONTROLDETAILS[cur_index]["txt_control_id"] == "") {
        var value = $.trim($("#ctl00_ContentPlaceHolder3_txt_name_" + cur_index).val()).replace(" ", "_");
        var SplChar = "=\\;.:,+*\'/!-\"#";
        //char[] trim = { '=', '\\', ';', '.', ':', ',', '+', '*', '\'', '/' };
        for (var i = 0; i < SplChar.length; i++) {
            if (value.indexOf(SplChar[i]) != -1)
                value = value.replace(SplChar[i], "");
        }
        $("#ctl00_ContentPlaceHolder3_txt_control_id").val(value);
        obj_json.CONTROLDETAILS[cur_index]["txt_control_id"] = value;
    }
    else
        $("#ctl00_ContentPlaceHolder3_txt_control_id").val(obj_json.CONTROLDETAILS[cur_index]["txt_control_id"]);

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_visible"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_visible"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_visible").val(obj_json.CONTROLDETAILS[cur_index]["ddl_visible"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_visible").val("1");
        obj_json.CONTROLDETAILS[cur_index]["ddl_visible"] = $("#ctl00_ContentPlaceHolder3_ddl_visible").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_is_read"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_is_read"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_is_read").val(obj_json.CONTROLDETAILS[cur_index]["ddl_is_read"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_is_read").val("0");
        obj_json.CONTROLDETAILS[cur_index]["ddl_is_read"] = $("#ctl00_ContentPlaceHolder3_ddl_is_read").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_in_list"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_in_list"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_in_list").val(obj_json.CONTROLDETAILS[cur_index]["ddl_in_list"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_in_list").val("1");
        obj_json.CONTROLDETAILS[cur_index]["ddl_in_list"] = $("#ctl00_ContentPlaceHolder3_ddl_in_list").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["txt_size"] != null && obj_json.CONTROLDETAILS[cur_index]["txt_size"] != "")
        $("#ctl00_ContentPlaceHolder3_txt_size").val(obj_json.CONTROLDETAILS[cur_index]["txt_size"]);
    else {
        $("#ctl00_ContentPlaceHolder3_txt_size").val("");
        obj_json.CONTROLDETAILS[cur_index]["txt_size"] = $("#ctl00_ContentPlaceHolder3_txt_size").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["txt_default_value"] != null && obj_json.CONTROLDETAILS[cur_index]["txt_default_value"] != "")
        $("#ctl00_ContentPlaceHolder3_txt_default_value").val(obj_json.CONTROLDETAILS[cur_index]["txt_default_value"]);
    else {
        $("#ctl00_ContentPlaceHolder3_txt_default_value").val("");
        obj_json.CONTROLDETAILS[cur_index]["txt_default_value"] = $("#ctl00_ContentPlaceHolder3_txt_default_value").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_is_unique"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_is_unique"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_is_unique").val(obj_json.CONTROLDETAILS[cur_index]["ddl_is_unique"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_is_unique").val("0");
        obj_json.CONTROLDETAILS[cur_index]["ddl_is_unique"] = $("#ctl00_ContentPlaceHolder3_ddl_is_unique").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_filter"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_filter"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_filter").val(obj_json.CONTROLDETAILS[cur_index]["ddl_filter"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_filter").val("0");
        obj_json.CONTROLDETAILS[cur_index]["ddl_filter"] = $("#ctl00_ContentPlaceHolder3_ddl_filter").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["txt_tooltip"] != null && obj_json.CONTROLDETAILS[cur_index]["txt_tooltip"] != "")
        $("#ctl00_ContentPlaceHolder3_txt_tooltip").val(obj_json.CONTROLDETAILS[cur_index]["txt_tooltip"]);
    else {
        $("#ctl00_ContentPlaceHolder3_txt_tooltip").val("");
        obj_json.CONTROLDETAILS[cur_index]["txt_tooltip"] = $("#ctl00_ContentPlaceHolder3_txt_tooltip").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_data_bind"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_data_bind"] != "")
        $("#ctl00_ContentPlaceHolder3_ddl_data_bind").val(obj_json.CONTROLDETAILS[cur_index]["ddl_data_bind"]);
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_data_bind").val("");
        obj_json.CONTROLDETAILS[cur_index]["ddl_data_bind"] = $("#ctl00_ContentPlaceHolder3_ddl_data_bind").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_format_mask"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_format_mask"] != "")
        $("#ctl00_ContentPlaceHolder3_ddl_format_mask").val(obj_json.CONTROLDETAILS[cur_index]["ddl_format_mask"]);
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_format_mask").val("");
        obj_json.CONTROLDETAILS[cur_index]["ddl_format_mask"] = $("#ctl00_ContentPlaceHolder3_ddl_format_mask").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_new_section"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_new_section"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_new_section").val(obj_json.CONTROLDETAILS[cur_index]["ddl_new_section"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_new_section").val("0");
        obj_json.CONTROLDETAILS[cur_index]["ddl_new_section"] = $("#ctl00_ContentPlaceHolder3_ddl_new_section").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_search"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_search"].toString() != "")
        $("#ctl00_ContentPlaceHolder3_ddl_search").val(obj_json.CONTROLDETAILS[cur_index]["ddl_search"] ? "1" : "0");
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_search").val("0");
        obj_json.CONTROLDETAILS[cur_index]["ddl_search"] = $("#ctl00_ContentPlaceHolder3_ddl_search").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["txt_section_name"] != null && obj_json.CONTROLDETAILS[cur_index]["txt_section_name"] != "")
        $("#ctl00_ContentPlaceHolder3_txt_section_name").val(obj_json.CONTROLDETAILS[cur_index]["txt_section_name"]);
    else {
        $("#ctl00_ContentPlaceHolder3_txt_section_name").val("");
        obj_json.CONTROLDETAILS[cur_index]["txt_section_name"] = $("#ctl00_ContentPlaceHolder3_txt_section_name").val();
    }

    if (obj_json.CONTROLDETAILS[cur_index]["ddl_field_type"] != null && obj_json.CONTROLDETAILS[cur_index]["ddl_field_type"] != "")
        $("#ctl00_ContentPlaceHolder3_ddl_field_type").val(obj_json.CONTROLDETAILS[cur_index]["ddl_field_type"]);
    else {
        $("#ctl00_ContentPlaceHolder3_ddl_field_type").val("USR");
        obj_json.CONTROLDETAILS[cur_index]["ddl_field_type"] = $("#ctl00_ContentPlaceHolder3_ddl_field_type").val();
    }
    if (obj_json.CONTROLDETAILS[cur_index]["ta_droplist"] != null && obj_json.CONTROLDETAILS[cur_index]["ta_droplist"] != "")
        $("#ctl00_ContentPlaceHolder3_ta_droplist").val(obj_json.CONTROLDETAILS[cur_index]["ta_droplist"]);
    else {
        $("#ctl00_ContentPlaceHolder3_ta_droplist").val("");
        obj_json.CONTROLDETAILS[cur_index]["ta_droplist"] = $("#ctl00_ContentPlaceHolder3_ta_droplist").val();
    }
    document.getElementById('ctl00_ContentPlaceHolder3_hf_controldetails_json').value = JSON.stringify(obj_json);
}

function deleteResetControl(obj) {
    var cur_index = $(obj[0]).closest('td').parent()[0].sectionRowIndex - 1;
    var obj_json = new Array();
    if ($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val() == "") return;
    obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
    if (!obj_json.hasOwnProperty("CONTROLDETAILS")) return;
    if (obj_json.CONTROLDETAILS[cur_index] == null) return;

    $("#ctl00_ContentPlaceHolder3_txt_name_" + cur_index).val("");
    if ($("#ctl00_ContentPlaceHolder3_txt_name_" + cur_index).css("background-color") != "rgb(255, 255, 255)")
        $("#ctl00_ContentPlaceHolder3_txt_name_" + cur_index).css("background-color", "#FFFFFF");
    obj_json.CONTROLDETAILS[cur_index]["txt_name"] = "";

    $("#ctl00_ContentPlaceHolder3_txt_desc_" + cur_index).val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_desc"] = "";

    $("#ctl00_ContentPlaceHolder3_txt_seq_" + cur_index).val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_seq"] = "";

    $("#ctl00_ContentPlaceHolder3_ddl_ctr_type_" + cur_index).val("TB");
    obj_json.CONTROLDETAILS[cur_index]["ddl_ctr_type"] = "TB";

    $("#ctl00_ContentPlaceHolder3_ddl_data_type_" + cur_index).val("TX");
    obj_json.CONTROLDETAILS[cur_index]["ddl_data_type"] = "TX";

    $("#ctl00_ContentPlaceHolder3_txt_control_id").val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_control_id"] = "";

    $("#ctl00_ContentPlaceHolder3_ddl_visible").val("1");
    obj_json.CONTROLDETAILS[cur_index]["ddl_visible"] = $("#ctl00_ContentPlaceHolder3_ddl_visible").val();

    $("#ctl00_ContentPlaceHolder3_ddl_is_read").val("0");
    obj_json.CONTROLDETAILS[cur_index]["ddl_is_read"] = $("#ctl00_ContentPlaceHolder3_ddl_is_read").val();

    $("#ctl00_ContentPlaceHolder3_ddl_in_list").val("1");
    obj_json.CONTROLDETAILS[cur_index]["ddl_in_list"] = $("#ctl00_ContentPlaceHolder3_ddl_in_list").val();

    $("#ctl00_ContentPlaceHolder3_txt_size").val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_size"] = $("#ctl00_ContentPlaceHolder3_txt_size").val();

    $("#ctl00_ContentPlaceHolder3_txt_default_value").val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_default_value"] = $("#ctl00_ContentPlaceHolder3_txt_default_value").val();

    $("#ctl00_ContentPlaceHolder3_ddl_is_unique").val("0");
    obj_json.CONTROLDETAILS[cur_index]["ddl_is_unique"] = $("#ctl00_ContentPlaceHolder3_ddl_is_unique").val();

    $("#ctl00_ContentPlaceHolder3_ddl_filter").val("0");
    obj_json.CONTROLDETAILS[cur_index]["ddl_filter"] = $("#ctl00_ContentPlaceHolder3_ddl_filter").val();

    $("#ctl00_ContentPlaceHolder3_txt_tooltip").val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_tooltip"] = $("#ctl00_ContentPlaceHolder3_txt_tooltip").val();

    $("#ctl00_ContentPlaceHolder3_ddl_data_bind").val("");
    obj_json.CONTROLDETAILS[cur_index]["ddl_data_bind"] = $("#ctl00_ContentPlaceHolder3_ddl_data_bind").val();

    $("#ctl00_ContentPlaceHolder3_ddl_format_mask").val("");
    obj_json.CONTROLDETAILS[cur_index]["ddl_format_mask"] = $("#ctl00_ContentPlaceHolder3_ddl_format_mask").val();

    $("#ctl00_ContentPlaceHolder3_ddl_new_section").val("0");
    obj_json.CONTROLDETAILS[cur_index]["ddl_new_section"] = $("#ctl00_ContentPlaceHolder3_ddl_new_section").val();

    $("#ctl00_ContentPlaceHolder3_ddl_search").val("0");
    obj_json.CONTROLDETAILS[cur_index]["ddl_search"] = $("#ctl00_ContentPlaceHolder3_ddl_search").val();

    $("#ctl00_ContentPlaceHolder3_txt_section_name").val("");
    obj_json.CONTROLDETAILS[cur_index]["txt_section_name"] = $("#ctl00_ContentPlaceHolder3_txt_section_name").val();

    $("#ctl00_ContentPlaceHolder3_ddl_field_type").val("USR");
    obj_json.CONTROLDETAILS[cur_index]["ddl_field_type"] = $("#ctl00_ContentPlaceHolder3_ddl_field_type").val();

    $("#ctl00_ContentPlaceHolder3_ta_droplist").val("");
    obj_json.CONTROLDETAILS[cur_index]["ta_droplist"] = $("#ctl00_ContentPlaceHolder3_ta_droplist").val();

    document.getElementById('ctl00_ContentPlaceHolder3_hf_controldetails_json').value = JSON.stringify(obj_json);
    $("#ctl00_ContentPlaceHolder3_img_delete_" + cur_index).css("display", "none");
}

function validateEmptyName(btn) {
    var ret = true;
    $(btn).val("Saving...");
    btn.disabled = true;
    var img_del = $("img[id^=ctl00_ContentPlaceHolder3_img_delete_]:visible");
    if (img_del.length > 0) {
        for (var i = 0; i < img_del.length; i++) {
            var img_index = $(img_del[i]).closest('td').parent()[0].sectionRowIndex - 1;
            if ($("#ctl00_ContentPlaceHolder3_txt_name_" + img_index).val() == "") {
                //$("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbErrorMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Control name has empty value at row number " + (img_index + 1) + ".</div>");
                $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbErrorMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Control name has empty value.</div>");
                //$("#ctl00_ContentPlaceHolder3_lblMessage").slideDown("fast").delay(5000).slideUp("fast");
                $("#ctl00_ContentPlaceHolder3_lblMessage").css("display", "block");
                $("#ctl00_ContentPlaceHolder3_txt_name_" + img_index).css("background-color", "#FFCCCC");
                //break;
                ret = false;
            }
        }
    }
    else {
        var txt_name = $("input[id^=ctl00_ContentPlaceHolder3_txt_name_]");
        var check_name = false;
        for (var i = 0; i < txt_name.length; i++) {
            if (txt_name[i].value != "")
                check_name = true;
        }
        if (!check_name) {
            if ($("#ctl00_ContentPlaceHolder3_txt_page_name").val() != "") {
                $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbErrorMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;At least one control is required.</div>");
                $("#ctl00_ContentPlaceHolder3_lblMessage").css("display", "block");
            }
            $("#ctl00_ContentPlaceHolder3_txt_name_0").css("background-color", "#FFCCCC");
            ret = false;
        }
    }

    if ($("#ctl00_ContentPlaceHolder3_txt_page_name").val() == "")
        ret = false;
//        $("#ctl00_ContentPlaceHolder3_lbl_pageid").text("This is required field !");
    if (ret)
        ret = validateDroplistValue();
    $(btn).val("Save");
    btn.disabled = false;
    
    return ret;
}

function disableThisButton(obj, newCaption)//used to disable buttons after click - sanal 22 Apr 2008
{
    document.getElementById(obj).value = newCaption;
    document.getElementById(obj).disabled = true;
}

function validateDroplistValue() {
    var ret = true;
    obj_json = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hf_controldetails_json").val());
    var img_del = $("img[id^=ctl00_ContentPlaceHolder3_img_delete_]:visible");
    if (img_del.length > 0) {
        for (var i = 0; i < img_del.length; i++) {
            var img_index = $(img_del[i]).closest('td').parent()[0].sectionRowIndex - 1;
            if ($("#ctl00_ContentPlaceHolder3_ddl_ctr_type_" + img_index).val() == "DL") {
                if (obj_json.CONTROLDETAILS[img_index]["ta_droplist"] == "" || (obj_json.CONTROLDETAILS[img_index]["ddl_data_bind"] != null && obj_json.CONTROLDETAILS[img_index]["ddl_data_bind"] != "")) {
                    $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbErrorMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Droplist Value does not support Empty or Data Bind value.</div>");
                    $("#ctl00_ContentPlaceHolder3_lblMessage").css("display", "block");
                    $("#ctl00_ContentPlaceHolder3_ddl_ctr_type_" + img_index).css("background-color", "#FFCCCC");
                    //break;
                    ret = false;
                }
            }
        }
    }
    return ret;
}

function showDroplistTooltip() {
    xOffset = 0;
    yOffset = 20;
    var pos = $("#ctl00_ContentPlaceHolder3_lbl_droplist").offset(); 
    $("#div_help_droplist")
            .css("top", (pos.top + yOffset) + "px")
            .css("left", (pos.left - xOffset) + "px")
            .fadeIn("fast");
}